import axios from 'axios';
import router from '@/router';
import { message } from 'ant-design-vue';

const actions = {
  login: async (context, payload) => {
    try {
      const res = await axios.post('/api/login', payload.data);
      sessionStorage.setItem('user_info', JSON.stringify(res.data));
      if (JSON.stringify(res.data) !== '{}') {
        const id = res.data.id;
        context.commit('getInfos', {id})
        message.success('登录成功')
        router.push('/');
      }
    } catch(e){
      message.error('登录失败')
    }
  },
  getInfos: async (context, payload) => {
    const menus = await axios.get(`/api/users/${payload.id}/navMenus`);
    context.commit('getMenus', menus.data);
  }
}

export default actions;